< previous page page_632 next page >

Page 632
In the sample code, the Recordset.Fields(field name) syntax is used. However, for some purposes (such as when you do not know the field name), using the field index is more appropriate. Field indexes start at zero and end at one less than the number of fields in the recordset. The number of fields in a recordset is available through the Count property of the Fields collection. The following code sample prints each field name and then scrolls through the recordset printing each field value:
'PRINT THE FIELD NAMES
For i = 0 To rs.Fields.Count - 1
    Debug.Print rs.Fields(i).Name,
Next i
Debug.Print

'PRINT CONTENTS OF ALL FIELDS IN EACH RECORD
rs.MoveFirst
While Not rs.EOF
    For i = 0 To rs.Fields.Count - 1
        Debug.Print rs.Fields(i),
    Next i
    Debug.Print
    rs.MoveNext
Wend
This sample code uses two properties of the Field object: the Name and the Value properties. Notice that you do not have to explicitly specify the Value property because it is the default property.
Recordset Navigation
After data has been retrieved into a Recordset object, you can access and update the values of fields in the current record. Think of the recordset as a long sequential file. At any given time, the current record is a pointer to a location within that file. To work with different records, you can use the following navigation methods to change the current record:
MoveFirstMoves to the first record, just after the beginning-of-file marker (BOF)
MoveLastMoves to the last record, just before the end-of-file marker (EOF)
MoveNextMoves to the next record after the current record (toward EOF)
MovePreviousMoves back to the record before the current record (toward BOF)
MoveMoves forward or backward a specified number of records
BOF and EOF are properties that indicate the beginning and ending points of a recordset, respectively.
Navigation and the CursorType Property
In the examples up to this point, you have seen only two methods used to navigate within a recordset: the MoveFirst and MoveNext methods. In Listing 28.1, you used a While loop to move forward through the records in an open recordset.
As a matter of fact, attempting to use MovePrevious or MoveLast with Listing 28.1 would cause an error, because it is invalid for the selected cursor type. A cursor in a recordset is like a cursor on a computer screen; it is a pointer to a current position.

 
< previous page page_632 next page >